package com.hitqz.robot.arm;

import com.hitqz.robot.api.business.enums.BrandEnum;
import com.hitqz.robot.arm.dto.*;
import com.hitqz.robot.arm.util.RokaeToolkit;
import com.hitqz.robot.camera.IEquipmentService;
import com.hitqz.robot.camera.configure.EquipmentCmdEnum;
import com.hitqz.robot.camera.dto.EquipmentLoginParamsDto;
import com.hitqz.robot.camera.dto.IEquipmentCmdParamsDto;
import com.hitqz.robot.camera.dto.IEquipmentCmdResult;
import com.hitqz.robot.camera.dto.QueryParamsDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

/**
 * @author kehanjiang
 * @date 2024/10/25
 */
@Service("rokaeArm")
@Slf4j
public class RokaeArmService implements IEquipmentService {

    private RokaeToolkit toolkit;
    private String ip;

    @Override
    public BrandEnum brand() {
        return BrandEnum.ROKAE;
    }

    @Override
    public boolean intSdk() {
        toolkit = RokaeToolkit.getInstance();
        return true;
    }

    @Override
    public boolean login(EquipmentLoginParamsDto dto) {
        ip = dto.getIp();
        return toolkit.connect(dto.getIp());
    }

    @Override
    public boolean loginout(String ip) {
        return toolkit.disconnect(ip);
    }

    @Override
    public IEquipmentCmdResult executeCmd(QueryParamsDto dto, boolean async, EquipmentCmdEnum cmdEnum, IEquipmentCmdParamsDto cmdParamsDto) {
        Assert.hasText(ip, "请先登录！");
        IEquipmentCmdResult result = new IEquipmentCmdResult();
        switch (cmdEnum) {
            case ROKAE_SET_POWER_STATE -> {
                PowerSwitchDto paramDto = (PowerSwitchDto) cmdParamsDto;
                if (paramDto.isOn()) {
                    toolkit.powerOn(ip);
                } else {
                    toolkit.powerOff(ip);
                }
            }
            case ROKAE_SET_OPERATE_MODE -> {
                OperateSwitchDto paramDto = (OperateSwitchDto) cmdParamsDto;
                toolkit.setOperateMode(ip, paramDto.getMode());
            }
            case ROKAE_GET_TCP_POSITION -> result = toolkit.getTcpPosition(ip);
            case ROKAE_GET_JOINT_POSITION -> result = toolkit.getJointPosition(ip);
            case ROKAE_JOINT_MOVE -> toolkit.jointMove(ip, (JointMoveDto) cmdParamsDto);
            case ROKAE_LINEAR_MOVE -> toolkit.linearMove(ip, (LinearMoveDto) cmdParamsDto);
            case ROKAE_CIRCULAR_MOVE -> toolkit.circularMove(ip, (CircularMoveDto) cmdParamsDto);
            case ROKAE_JOG -> toolkit.jog(ip, (JogMoveDto) cmdParamsDto);
            case ROKAE_MOTION_ABORT -> toolkit.motionAbort(ip);
            case ROKAE_GET_ROBOT_STATUS -> result = toolkit.getRobotStatus(ip);
            case ROKAE_RECOVER_STATE -> toolkit.recoverState(ip);
            case ROKAE_SET_DRAG_STATE -> {
                DragSwitchDto paramDto = (DragSwitchDto) cmdParamsDto;
                if (paramDto.isOn()) {
                    toolkit.enableDrag(ip);
                } else {
                    toolkit.disableDrag(ip);
                }
            }
            case ROKAE_SET_RECORD_PATH_STATE -> {
                RecordPathSwitchDto paramDto = (RecordPathSwitchDto) cmdParamsDto;
                int type = paramDto.getType();
                String path = paramDto.getPath();
                String saveAsName = paramDto.getSaveAsName();
                double rate = paramDto.getRate();
                boolean removeAll = paramDto.isRemoveAll();
                switch (type) {
                    case 0 -> toolkit.startRecordPath(ip);
                    case 1 -> toolkit.stopRecordPath(ip);
                    case 2 -> toolkit.cancelRecordPath(ip);
                    case 3 -> {
                        Assert.hasText(path, "路径名称path不可为空");
                        if (StringUtils.hasText(saveAsName)) {
                            toolkit.saveRecordPath(ip, path, saveAsName);
                        } else {
                            toolkit.saveRecordPath(ip, path);
                        }
                    }
                    case 4 -> {
                        Assert.hasText(path, "路径名称path不可为空");
                        toolkit.replayPath(ip, path, rate);
                    }
                    case 5 -> {
                        Assert.hasText(path, "路径名称path不可为空");
                        toolkit.removePath(ip, path, removeAll);
                    }
                }
            }

        }
        return result;
    }

}
